//
//  SinglePieceControllerd.m
//  Gift
//
//  Created by dllo on 15/9/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SinglePieceControllerd.h"
#import "BaseHeader.h"

@interface SinglePieceControllerd ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionViewOne;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic ,assign)NSInteger page;

@end

@implementation SinglePieceControllerd


-(void)dealloc{
    
    self.collectionViewOne.delegate = nil;
    self.collectionViewOne.dataSource = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.collectionViewOne release];
    [self.array release];
    [super dealloc];
}

- (void)handleData:(NSInteger)number{
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?gender=1&generation=0&limit=50&offset=%ld",self.page];
    [PFAFNetworking getUrl: str body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        if (number == 0) {
            self.array = [[NSMutableArray array]init];
        }
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"items"]) {
            NSDictionary *dic2 = [dic objectForKey:@"data"];
            SinglePieceModel *model = [SinglePieceModel modelSetDic:dic2];
            [self.array addObject:model];

        }
        
        [self.collectionViewOne reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
   
        
    }];
    
    
}







- (void)cratercollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

        //最小间距设置
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.itemSize = CGSizeMake((SCREEN_WIDTH / 2) - 15, (SCREEN_HEIGHT / 3 ) + 20);
//    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
        //创建瀑布流
    self.collectionViewOne = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];

    
        //自动翻页
    [self.collectionViewOne setPagingEnabled:YES];
    
        //写个重用池
    [self.collectionViewOne registerClass:[LDCollectionViewCell class] forCellWithReuseIdentifier:@"collectionHot"];
        self.collectionViewOne.backgroundColor = [UIColor whiteColor];
        //签订代理
    self.collectionViewOne.dataSource = self;
    self.collectionViewOne.delegate = self;

        //加到view上
    [self.view addSubview:self.collectionViewOne];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
//
    [layout release];
 
    //夜间
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        self.collectionViewOne.backgroundColor = [UIColor blackColor];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"lidi" object:nil];
    
}

//夜间执行方法
- (void)handleNotification:(NSNotification *)Notification{
    
    NSString *passWord = [Notification object];
    if ([passWord isEqualToString:@"yes"]) {
        self.collectionViewOne.backgroundColor = [UIColor blackColor];
    }else if ([passWord isEqualToString:@"no"]){
        self.collectionViewOne.backgroundColor = [UIColor whiteColor];
    }
    
}




//瀑布流代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionHot" forIndexPath:indexPath];
    
    SinglePieceModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
}

//pushu方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SinglePieceModel *model = [self.array objectAtIndex:indexPath.row];
    LDSingieceWeb *singieceWeb = [[LDSingieceWeb alloc]init];
    singieceWeb.strWeb = model.url;

    [self.navigationController pushViewController:singieceWeb animated:YES];

    [singieceWeb release];
}





//下拉刷新
-(void)addHeader
{
    self.collectionViewOne.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handleData:self.page];
    }];
    
    [self.collectionViewOne.header beginRefreshing];
}
//上拉加载
-(void)addFooter
{
    self.collectionViewOne.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.page = self.page + 50;
        [self handleData: self.page];
    }];
    
    
}


-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"单品"];
    
    
    self.array = [NSMutableArray array];
    [self cratercollectionView];
    self.page = 0;//默认请求下来的数据为第0页
    [self handleData:self.page];
    [self addHeader];
    [self addFooter];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
